package demisemapping;

import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Macros {
    public static int KEY_DELAY_MILLIS = 32;
        
    public static void tapKey(Robot r, int c) throws Exception {
        r.keyPress(c);
        r.keyRelease(c);
        Thread.sleep(KEY_DELAY_MILLIS);
    }
    
    public static void type(Robot r, String s) throws Exception {
        String x = s.toLowerCase();
        for (int i=0;i<x.length();i++) {
            int key = -1;
            switch (x.charAt(i)) {
                case 'a': key = KeyEvent.VK_A; break;
                case 'b': key = KeyEvent.VK_B; break;
                case 'c': key = KeyEvent.VK_C; break;
                case 'd': key = KeyEvent.VK_D; break;
                case 'e': key = KeyEvent.VK_E; break;
                case 'f': key = KeyEvent.VK_F; break;
                case 'g': key = KeyEvent.VK_G; break;
                case 'h': key = KeyEvent.VK_H; break;
                case 'i': key = KeyEvent.VK_I; break;
                case 'j': key = KeyEvent.VK_J; break;
                case 'k': key = KeyEvent.VK_K; break;
                case 'l': key = KeyEvent.VK_L; break;
                case 'm': key = KeyEvent.VK_M; break;
                case 'n': key = KeyEvent.VK_N; break;
                case 'o': key = KeyEvent.VK_O; break;
                case 'p': key = KeyEvent.VK_P; break;
                case 'q': key = KeyEvent.VK_Q; break;
                case 'r': key = KeyEvent.VK_R; break;
                case 's': key = KeyEvent.VK_S; break;
                case 't': key = KeyEvent.VK_T; break;
                case 'u': key = KeyEvent.VK_U; break;
                case 'v': key = KeyEvent.VK_V; break;
                case 'w': key = KeyEvent.VK_W; break;
                case 'x': key = KeyEvent.VK_X; break;
                case 'y': key = KeyEvent.VK_Y; break;
                case 'z': key = KeyEvent.VK_Z; break;
                case ' ': key = KeyEvent.VK_SPACE; break;
                case '0': key = KeyEvent.VK_0; break;
                case '1': key = KeyEvent.VK_1; break;
                case '2': key = KeyEvent.VK_2; break;
                case '3': key = KeyEvent.VK_3; break;
                case '4': key = KeyEvent.VK_4; break;
                case '5': key = KeyEvent.VK_5; break;
                case '6': key = KeyEvent.VK_6; break;
                case '7': key = KeyEvent.VK_7; break;
                case '8': key = KeyEvent.VK_8; break;
                case '9': key = KeyEvent.VK_9; break;
                case ',': key = KeyEvent.VK_COMMA; break;
                case '-': key = KeyEvent.VK_MINUS; break;
                case '\n': key = KeyEvent.VK_ENTER; break;
                case '\t': key = KeyEvent.VK_TAB; break;
                default: break;
            }
            assert(key != -1);
            tapKey(r, key);
        }
    }
    
    private static void castSpell(Robot r, String spell_name, String name)
    throws Exception {
        type(r, "c"+spell_name+"\n"+name+"\n");
    }
    
    static void castSpells1(Robot r) throws Exception {
        castSpell(r, "mental resistance", "oxbull");
        castSpell(r, "resist stoning", "oxbull");
        type(r, "\t");
    }
    
    static void castSpells2(Robot r) throws Exception {
        castSpell(r, "resist fire", "tetsu");
        castSpell(r, "resist cold", "tetsu");
        castSpell(r, "mental resistance", "tetsu");
        castSpell(r, "resist disease", "tetsu");
        castSpell(r, "resist stoning", "tetsu");
        castSpell(r, "resist paralysis", "tetsu");
        castSpell(r, "see invisible", "tetsu");
        castSpell(r, "charm of concealment", "tetsu");
        type(r, "\t");
    }
    
    static void castSpells3(Robot r) throws Exception {
        castSpell(r, "resist fire", "trbot");
        castSpell(r, "resist cold", "trbot");
        castSpell(r, "mental resistance", "trbot");
        castSpell(r, "resist disease", "trbot");
        castSpell(r, "resist poison", "trbot");
        castSpell(r, "see invisible", "trbot");
        castSpell(r, "charm of concealment", "trbot");
        type(r, "\t");
    }
    
    static void castSpells4(Robot r) throws Exception {
        castSpell(r, "resist fire", "madou");
        castSpell(r, "resist cold", "madou");
        castSpell(r, "mental resistance", "madou");
        castSpell(r, "resist disease", "madou");
        castSpell(r, "resist poison", "madou");
        castSpell(r, "resist magic", "madou");
        castSpell(r, "resist stoning", "madou");
        type(r, "\t");
    }
    
    static void teleport(Robot r, Location offset) throws Exception {
        // j is RIGHT, i is FORWARD, k is DOWN
        type(r, "u2\n");
        Thread.sleep(500);
        type(r, offset.j + "," + offset.i + "," + offset.k);
    }

}
